import 'package:bottom_sheet_flutter/utils/color_util/color_util.dart';
import 'package:bottom_sheet_flutter/utils/object_util/object_util.dart';
import 'package:bottom_sheet_flutter/utils/style_util/style_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';

import '../../widgets/text_widget/text_widget.dart';

/// @className : gold_coins_give_bottom_sheet.dart
/// @date 2022/07/17 14:43
/// @author : xiaohai
/// @description : 金币赠送抽屉层弹框

class GoldCoinsGiveBottomSheetView extends StatefulWidget {


  GoldCoinsGiveBottomSheetView({Key? key,this.giveClick,this.userId,this.giveNumber}) : super(key: key);

  Function(String? userId, String? giveNumber)? giveClick;
  //对方账户id
  RxString? userId = "".obs;
  //赠送数量
  RxString? giveNumber = "".obs;

  @override
  _GoldCoinsGiveBottomSheetViewState createState() =>
      _GoldCoinsGiveBottomSheetViewState();
}

class _GoldCoinsGiveBottomSheetViewState
    extends State<GoldCoinsGiveBottomSheetView> {

  //赠送按钮状态
  RxBool? giveBtnState = false.obs;
  //对方账户id文本框
  late TextEditingController? idEditingController = TextEditingController();
  //赠送数量文本框
  late TextEditingController? numberEditingController = TextEditingController();


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    idEditingController!.text = widget.userId!.value;
    numberEditingController!.text = widget.giveNumber!.value;
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      padding: EdgeInsets.only(
        left: 32.w,
        right: 32.w,
        //bottom: MediaQuery.of(context).viewInsets.bottom
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(height: 32.w),
          buildTitleWidget(),
          SizedBox(height: 64.w),
          buildAccountWidget(),
          SizedBox(height: 32.w),
          buildGiveNumberWidget(),
          SizedBox(height: 48.w),
          buildTipWidget(),
          SizedBox(height: 90.w),
          buildGiveWidget(),
          SizedBox(height: 100.w),
        ],
      ),
    );
  }

  /// 构建title组件
  Widget buildTitleWidget() {
    return Container(
      height: 48.w,
      alignment: Alignment.center,
      child: textWidget("金币赠送", TS_3E3752_40_W5)
    );
  }

  /// 构建对方账户组件
  Widget buildAccountWidget() {
    return Builder(builder: (context) {
      return Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              height: 44.w,
              child: textWidget("对方账户", TS_3E3752_32_W5)
            ),
            SizedBox(height: 16.w),
            Container(
              height: 80.w,
              decoration: BoxDecoration(
                color: ColorsUtil.hexColor(0xF4F2FC),
                borderRadius: BorderRadius.all(Radius.circular(16.w)),
              ),
              padding: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 3.w),
              child: TextField(
                controller: idEditingController,
                style: TextStyle(
                    color: ColorsUtil.hexColor(0xff3E3752),
                    fontSize: 32.sp,
                    fontWeight: FontWeight.w500),
                inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                keyboardType: TextInputType.number,
                //focusNode: bloc.state.idFocusNode,
                decoration: InputDecoration(
                    hintText: '请输入对方ID',
                    hintStyle: TextStyle(
                        color: ColorsUtil.hexColor(0xff3E3752),
                        fontSize: 28.sp,
                        fontWeight: FontWeight.w400,
                        height: 2.6.w),
                    border: InputBorder.none),
                //点击键盘上完成
                onSubmitted: (value) async {
                  // if (ObjectUtil.isNotEmpty(value)) {
                  //   bloc.getUserNameByUserNo(bloc.state.userId);
                  // }
                },
                //当文本框值发生变化时触发
                onChanged: (value) {
                  widget.userId!.value = value;
                  setGiveBtnState();
                },
              ),
            )
          ],
        ),
      );
    });
  }

  /// 构建对赠送数量组件
  Widget buildGiveNumberWidget() {
    return Builder(builder: (context) {
      return Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
                height: 44.w,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    textWidget('赠送数量', TS_3E3752_32_W5),
                    Row(
                      children: [
                        textWidget('', TS_948EA3_24_W5)
                      ],
                    )
                  ],
                )),
            SizedBox(height: 16.w),
            Container(
              height: 80.w,
              decoration: BoxDecoration(
                color: ColorsUtil.hexColor(0xF4F2FC),
                borderRadius: BorderRadius.all(Radius.circular(16.w)),
              ),
              padding: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 3.w),
              child: TextField(
                controller: numberEditingController,
                inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                keyboardType: TextInputType.number,
                //focusNode: bloc.state.numberFocusNode,
                style: TextStyle(
                    color: ColorsUtil.hexColor(0xff3E3752),
                    fontSize: 32.sp,
                    fontWeight: FontWeight.w500),
                decoration: InputDecoration(
                    hintText: '请输入赠送的金币数量',
                    hintStyle: TextStyle(
                        color: ColorsUtil.hexColor(0xff3E3752),
                        fontSize: 28.sp,
                        fontWeight: FontWeight.w400,
                        height: 2.7.w),
                    border: InputBorder.none),
                onSubmitted: (value) {},
                //当文本框值发生变化时触发
                onChanged: (value) {
                  widget.giveNumber!.value = value;
                  setGiveBtnState();
                },
              ),
            )
          ],
        ),
      );
    });
  }

  /// 构建提示组件
  Widget buildTipWidget() {
    return Container(
        child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        textWidget("提示\n1.赠送不可低于10000金币； \n2.请谨慎操作，贈送成功后不可赎回。", TS_3E3752_24_W5)
      ],
    ));
  }

  /// 构建赠送组件
  Widget buildGiveWidget() {
    return Builder(builder: (context) {
      return GestureDetector(
          onTap: () {
            if (ObjectUtil.isEmpty(widget.userId!.value)) {
              return;
            }

            if (ObjectUtil.isEmpty(widget.giveNumber!.value)) {
              return;
            }
            Get.close(1);

            widget.giveClick!(widget.userId!.value,widget.giveNumber!.value);

          },
          child: Obx(
            () => Container(
              height: 96.w,
              width: 686.w,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(16.w)),
                  gradient: LinearGradient(colors: [
                    ColorsUtil.hexColor(0x6A00FE,
                        alpha: (giveBtnState!.value ? 1 : 0.4)),
                    ColorsUtil.hexColor(0x8F40FE,
                        alpha: (giveBtnState!.value ? 1 : 0.4))
                  ], begin: Alignment.centerLeft, end: Alignment.centerRight)),
              child: textWidget("赠送",TS_FFFFFF_36_W4)
            ),
          ));
    });
  }

  /// 设置赠送按钮状态
  void setGiveBtnState() {
    if (ObjectUtil.isNotEmpty(widget.userId!.value) && ObjectUtil.isNotEmpty(widget.giveNumber!.value)) {
      giveBtnState!.value = true;
    } else {
      giveBtnState!.value = false;
    }
  }
}
